package com.navi.loaddata.core.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by admin on 2017/10/21.
 */
public class LoggerUtil {
    private Logger logger;
    private Class clazz;

    public LoggerUtil() {

    }

    public LoggerUtil(Class clazz) {
        this.logger = LoggerFactory.getLogger(clazz);
    }

    public static String stackTraceToString(Exception ex) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        ex.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

    public void printHeartBeartLog() {
        logger.info(" sending  heartbeat ....");
    }

    public void printStackTrace(Exception ex) {
        stackTraceToString(ex);
    }

    /**
     * 打印启动Log
     *
     * @param systemId 系统启动时分配的system id
     */
    public void pringLog(String systemId) {
        systemId = StringUtils.isEmpty(systemId) ? System.getProperty("SystemInstanceUUID") : systemId;
        logger.info("QMS-SPY: systemId:{} has started ...", systemId);
    }

    public void pringLog(String systemId, String message) {
        systemId = StringUtils.isEmpty(systemId) ? System.getProperty("SystemInstanceUUID") : systemId;
        logger.info("QMS-SPY: systemId:{} , message : {} ", systemId, message);
    }


    public void printLog(String systemId, String jobSeqId, String message) {
        systemId = StringUtils.isEmpty(systemId) ? System.getProperty("SystemInstanceUUID") : systemId;
        logger.info("QMS-SPY: systemId:{} , jobSeqId:{} . message: {}",
                systemId, jobSeqId, message);
    }

    public void printLog(String systemId, String jobSeqId, String indexFilePath, String message) {
        systemId = StringUtils.isEmpty(systemId) ? System.getProperty("SystemInstanceUUID") : systemId;
        logger.info("QMS-SPY: systemId:{} , jobSeqId:{} ,indexFilePath:{},  message: {}",
                systemId, jobSeqId, indexFilePath, message);

    }

    public void printLog(String systemId, String jobSeqId, String indexFilePath, String defectFileName, String message) {
        systemId = StringUtils.isEmpty(systemId) ? System.getProperty("SystemInstanceUUID") : systemId;
        logger.info("QMS-SPY: systemId:{} , jobSeqId:{} ,indexFilePath:{}, defectName : {}, message: {}",
                systemId, jobSeqId, indexFilePath, defectFileName, message);
    }

    public void printLog(String systemId, String jobSeqId, String indexFilePath, String defectName, String ruleId, String message) {
        systemId = StringUtils.isEmpty(systemId) ? System.getProperty("SystemInstanceUUID") : systemId;
        logger.info("QMS-SPY: systemId:{} , jobSeqId:{} ,indexFilePath:{}, defectName : {}, ruleId : {} , message: {}",
                systemId, jobSeqId, indexFilePath, defectName, ruleId, message);
    }

    public static void print(String id, String str, Object... params) {
        Object[] params2 = new Object[4];
        params2[0] = id;
        int start = 1;

        printfLog(str, params2, start, params);
    }

    private static void printfLog(String str, Object[] params2, int start, Object[] params) {
        final Logger logger = LoggerFactory.getLogger(LoggerUtil.class);
        for (int i = start; i <= params.length; i++) {
            params2[i] = params[i - 1];
        }

        logger.info("id : {} , " + str, params2);
    }

    public static void main(String[] args) {
        print("1", " {} ,{} , {} ", 2, "a", 3L);
    }

}
